﻿@using Microsoft.Extensions.Caching.Memory;
@inherits LayoutComponentBase
@inject NavigationManager Navigation
@inject IMemoryCache _memoryCache
<MApp>
	@if (Navigation.Uri == Navigation.BaseUri + "screen")
	{
		<div><Screen></Screen></div>
	}
	else
	{
		<CascadingValue Value="UserId">
			<MAppBar Elevation=0 App MaxHeight=48 Height=48
				 Class="@($"{(_pageTab == PageModes.PageTab ?"mx-6":"mx-6 rounded-b-4")}")">
				<Favorite />
				<MSpacer />
				<Login />
			</MAppBar>

			<Navigation />

			<MMain Class="fill-lighten-1">
				@if (_pageTab == PageModes.PageTab)
				{
					<PageTabs>
						@Body
					</PageTabs>
				}
				else
				{
					<div class="pa-6 max-width">
						<Breadcrumb></Breadcrumb>
						@Body
					</div>
				}
			</MMain>
			<Settings @bind-PageModel="_pageTab" @bind-Show=_showSetting />
		</CascadingValue>
	}
	<MOverlay Value="overlay">
		<MProgressCircular indeterminate
						   Size="20"></MProgressCircular>
						   正在初始化数据.......
	</MOverlay>
</MApp>

@code {
	public bool _showSetting;
	public string? _pageTab;
	public bool forceRender { get; set; } = false;
	public string UserId { get; set; }
	public bool overlay = true;
	[Inject]
	public IPopupService PopupService
	{
		get;
		set;
	}
	[CascadingParameter]
	public Task<AuthenticationState>? authenticationState { get; set; }
	protected async override Task OnInitializedAsync()
	{
		var usd = "";
		var islog = _memoryCache.TryGetValue("user", out usd);
		if (islog)
		{
			overlay = false;
			var authState = await authenticationState;
			var user = authState?.User;
			UserId = user.Claims.FirstOrDefault(it => it.Type == ClaimConst.UserId).Value;
			await PopupService.ToastSuccessAsync($"欢迎{user.Identity.Name} 进入系统！！");
			await InvokeAsync(StateHasChanged);
		}
		else
		{
			Navigation.NavigateTo($"{Navigation.BaseUri}pages/authentication/login-v2");
		}
	}
}